package com.xm.servlet.admin;
/**
* @Description: 管理员处理器
* @Author: Mr.Wang
* @Date: 2020/8/18
*/
import com.xm.biz.AdminBiz;
import com.xm.biz.impl.AdminBizImpl;
import com.xm.po.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/gly")
public class AdminServlet extends HttpServlet {
    //初始化数据层
    AdminBiz adminBiz = new AdminBizImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        if (type.equals("adminLogin")){
            //管理员登录方法
            adminLogin(request,response);
        }
        /*显示所有管理员信息*/
        else if (type.equals("adminInfo")){
            adminInfo(request,response);
        }
        /*删除管理员信息*/
        else if (type.equals("adminDelete")){
            adminDelete(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    //管理员登入
    public void adminLogin(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        Admin login = adminBiz.login(name, pwd);
        //判用户名码和密码是否一致
        if (login!=null){
            HttpSession session = request.getSession();
            session.setAttribute("admin",login);
            //response.sendRedirect("../admin/index.jsp");
            request.getRequestDispatcher("admin/index.jsp").forward(request,response);
            //http://localhost:8080/e_commerce/index.jsp
        }else {
            request.setAttribute("name",name);
            request.setAttribute("msg","用户名或密码错误");
            request.getRequestDispatcher("admin/login.jsp").forward(request,response);
        }
    }
    //显示所有管理员信息
    public void adminInfo(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        List<Admin> all = adminBiz.getAll();
        request.setAttribute("admins",all);
        request.getRequestDispatcher("admin/table_data_tables.jsp").forward(request,response);
    }
    //删除管理员信息
    public void adminDelete(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        boolean b = adminBiz.deleteById(id);
        if (b==true){

        }
    }
}
